// 合并区间  intervals = [[1,3],[2,6],[8,10],[15,18]] =》输出 [[1,6],[8,10],[15,18]]

/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var merge = function (intervals) {
  let result = []

  intervals.sort((a, b) => {
    return a[0] - b[0]
  })

  result.push(intervals[0])
  for (let i = 1; i < intervals.length; i++) {
    if (intervals[i][0] <= result[result.length - 1][1]) {
      if (intervals[i][1] >= result[result.length - 1][1]) {
        result[result.length - 1] = [result[result.length - 1][0], intervals[i][1]]
      }
    } else {
      result.push(intervals[i])
    }
  }

  return result
};

console.log(merge([[1, 4], [2, 3]]));
